package com.study.jwt.service;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.study.jwt.bean.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: Leo
 * @createDate: 2020/4/27
 * @version: 1.0
 */
@Service("TokenService")
public class TokenService
{
    private static final Log log= LogFactory.getLog(TokenService.class);
    public String getToken(User user) {
        String token="";
        token= JWT.create().withAudience(user.getId())// 将 user id 保存到 token 里面
                .sign(Algorithm.HMAC256(user.getPassword()));// 以 password 作为 token 的密钥
        log.info("{token}"+token);
        return token;
    }
}
